<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="交易对名称" prop="currencyPair">
        <el-input
          v-model="queryParams.currencyPair"
          placeholder="请输入交易对名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="调用状态" prop="callingState">
        <el-select v-model="queryParams.callingState" placeholder="请选择调用状态" clearable>
          <el-option
            v-for="dict in dict.type.statues"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sp_api_creat_order:sp_api_creat_order_bak:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sp_api_creat_order_bakList" @selection-change="handleSelectionChange" stripe>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="交易对名称" align="center" prop="currencyPair" />
      <el-table-column label="调用状态" align="center" prop="callingState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.statues" :value="scope.row.callingState"/>
        </template>
      </el-table-column>
      <el-table-column label="最后执行时间" align="center" prop="lastExecutionTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.lastExecutionTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成功次数" align="center" prop="successTimes" />
      <el-table-column label="失败次数" align="center" prop="failTimes" />
      <el-table-column
      <el-table-column label="失败原因(成功后显示历史失败的原因)" align="center" prop="lastFailureReason" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改创建新订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSp_api_creat_order_bak, getSp_api_creat_order_bak, delSp_api_creat_order_bak, addSp_api_creat_order_bak, updateSp_api_creat_order_bak } from "@/api/sp_api_creat_order/sp_api_creat_order_bak";

export default {
  name: "Sp_api_creat_order_bak",
  dicts: ['statues'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 创建新订单表格数据
      sp_api_creat_order_bakList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        currencyPair: null,
        callingState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询创建新订单列表 */
    getList() {
      this.loading = true;
      listSp_api_creat_order_bak(this.queryParams).then(response => {
        this.sp_api_creat_order_bakList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        currencyPair: null,
        callingState: null,
        lastExecutionTime: null,
        successTimes: null,
        failTimes: null,
        lastFailureReason: null,
        dangerWarning: null,
        safeWarning: null,
        larkAlarm: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加创建新订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSp_api_creat_order_bak(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改创建新订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSp_api_creat_order_bak(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSp_api_creat_order_bak(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除创建新订单编号为"' + ids + '"的数据项？').then(function() {
        return delSp_api_creat_order_bak(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sp_api_creat_order/sp_api_creat_order_bak/export', {
        ...this.queryParams
      }, `sp_api_creat_order_bak_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
